random.py

Defines RandomGen, a wrapper around all common functions relying on randomness.

class bashfuscator.core.engine.random.RandomGen[source]

Wrapper around random.SystemRandom. Provided for ease of use and to avoid having to initialize a SystemRandom object every time something random is desired.

Note

The default character set when generating random variable names or strings is the alphanumeric charset, or the (almost) full ASCII charset if setFullAsciiStrings() is called.

setFullAsciiStrings()[source]

Set the default charset used when generating random variables and strings to the (almost) full ASCII charset. Only “’” and “/” are not used.

forgetUniqueStrs()[source]

Clear the sets of previously generated variable names and strings. Should be called when random variable names/strings are needed but can have the same name as previously generated variable names/strings without causing conflicts.

randGenNum(min, max)[source]

Randomly generate an integer inclusively.

Parameters:
  • min (int) – minimum integer that can be returned
  • max (int) – maximum integer that can be returned
randChoice(max)[source]

Generate a random choice. Useful when you need to choose between a set number of choices randomly.

Parameters:max – maximum integer that can be returned
Returns:integer from 0 to max-1 inclusively
probibility(prob)[source]

Return True a certain percentage of the time.

Parameters:prob (int) – probability of returning True
Returns:True prob percent of the time, False otherwise
randSelect(seq)[source]

Randomly select an element from a sequence. If the argument ‘seq’ is a dict, a randomly selected key will be returned.

Parameters:seq (list) – sequence to randomly select from
Returns:element from seq if seq is a list, a key if seq is a dict, or None if seq is empty
randShuffle(seq)[source]

Randomly shuffle a sequence in-place.

Parameters:seq (list) – sequence to shuffle randomly
randGenVar(minVarLen=None, maxVarLen=None)[source]

Generate a unique randomly named variable. Variable names can consist of uppercase and lowercase letters, digits, and underscores, but will always start with a letter or underscore.

Parameters:sizePref (int) – sizePref user option. Controls the minimum and maximum length of generated variable names
Returns:unique random variable name

Note

randUniqueStr() is called under the hood, therefore the same performance concerns apply.

randUniqueStr(minStrLen=None, maxStrLen=None, charList=None, escapeChars='', noBOBL=True)[source]

Generate a random string that is guaranteed to be unique.

Parameters:
  • minStrLen (int) – minimum length of generated string
  • maxStrLen (int) – maximum length of generated string
  • charList (str or list of chrs) – list of characters that will be used when generating the random string. If it is not specified, the default character set will be used
Returns:

unique random string

Note

Runtime will increase incrementally as more and more unique strings are generated, unless forgetUniqueStrs() is called.

randGenStr(minStrLen=None, maxStrLen=None, charList=None, escapeChars='', noBOBL=True)[source]

Generate a random string. Functions the same as randUniqueStr(), the only difference being that the generated string is NOT guaranteed to be unique.